package com.tender.controller;

import com.tender.LoginBizService;
import com.tender.dto.LoginDTO;
import com.tender.common.Result;
import com.tender.enums.ErrorTypeEnum;
import com.tender.exceptions.CommonException;
import com.tender.utils.PicCodeUtil;
import com.tender.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api/v1/ws/op")
@Slf4j
public class LonginController {

    @Autowired
    private LoginBizService loginBizService;

    @PostMapping("/login")
    public Result<String> submit(@RequestBody LoginDTO param) {
        Result<String> success = Result.success();
        success.setData(loginBizService.submit(param));
         return success;
    }

    @GetMapping("/parse")
    public Result<LoginDTO > parse() {
        return Result.success(loginBizService.parse());
    }

    @GetMapping("/picCode/{size}/{timestamp}")
    public void picCode(@PathVariable("size") Integer size, @PathVariable("timestamp") String timestamp) {
        HttpServletResponse response = ServletUtils.getResponse();
        PicCodeUtil codeUtil = new PicCodeUtil();
        String picCode = codeUtil.getPicCode(response, size);
        log.info("=============== 图形验证码 key：{} =============== ", timestamp);
        log.info("=============== 图形验证码 picCode：{} =============== ", picCode);
        // TODO redis 中（已经存在session 中了）
//        stringRedisTemplate.opsForValue().set(timestamp, picCode);
//        stringRedisTemplate.expire(timestamp, configProperties.getPicCodeExpireTimes(), TimeUnit.SECONDS);
    }

}
